package com.hemaapp.yjnh.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.uu.utils.uuUtils;
import com.hemaapp.yjnh.bean.ClientInfor;
import com.hemaapp.yjnh.bean.EventBusConfig;
import com.hemaapp.yjnh.bean.EventBusMsg;
import com.hemaapp.yjnh.bean.User;
import com.hemaapp.hm_FrameWork.HemaNetTask;
import com.hemaapp.hm_FrameWork.result.HemaArrayResult;
import com.hemaapp.hm_FrameWork.result.HemaBaseResult;
import com.hemaapp.yjnh.BaseActivity;
import com.hemaapp.yjnh.BaseApplication;
import com.hemaapp.yjnh.BaseHttpInformation;
import com.hemaapp.yjnh.R;

import de.greenrobot.event.EventBus;
import de.greenrobot.event.Subscribe;
import xtom.frame.util.XtomToastUtil;

/**我的粮票
 * @author ZhangGuiyou
 * Date: 2015-7-31 15:25
 */
public class FoodStampActivity extends BaseActivity implements OnClickListener{

	private ImageButton titleLeft;
	private TextView title;
	private Button right;
	private ImageView titleRight;

	private TextView total_fee;// 余额
	private TextView coupons_hint;//粮票为0时的提示文字
	private TextView tv_get_records;// 充值记录
	private TextView tv_pay_records;// 提现记录
	private LinearLayout ll_family;// 提现记录
	private LinearLayout ll_zhuanzeng;// 账户充值按钮
	private LinearLayout ll_chongzhi;//
	private TextView tv_family_count;//家族人数

	private User user;
	private ClientInfor infor;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_coupons);
		user = BaseApplication.getInstance().getUser();
		super.onCreate(savedInstanceState);
		EventBus.getDefault().register(this);
		init();
	}
	
	private void init() {
		if (user != null) {
			getNetWorker().clientInfor(user.getToken());
		}
	}

	@Override
	public void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}
	@Override
	protected void callBeforeDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ACCOUNT_GET:
			showProgressDialog("请稍候");
			break;
		default:
			break;
		}
	}

	@Override
	protected void callAfterDataBack(HemaNetTask netTask) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ACCOUNT_GET:
			cancelProgressDialog();
			break;
		default:
			break;
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	protected void callBackForServerSuccess(HemaNetTask netTask,
			HemaBaseResult baseResult) {
		BaseHttpInformation information = (BaseHttpInformation) netTask
				.getHttpInformation();
		switch (information) {
		case ACCOUNT_GET:
			HemaArrayResult<ClientInfor> cResult = (HemaArrayResult<ClientInfor>) baseResult;
			infor = cResult.getObjects().get(0);
			total_fee.setText(uuUtils.formatAfterDot2(infor.getScore()));
			tv_family_count.setText(infor.getMember_count()+"人");
            int billCount = 0;
			double score = 0.0;
            try{
				billCount = Integer.parseInt(infor.getCost_billcount());
				score = Double.parseDouble(infor.getScore());
				if (score <= 0)//解决后台返回负数等奇葩问题
					total_fee.setText("0.00");
				else
					total_fee.setText(uuUtils.formatAfterDot2(infor.getScore()));
            }catch (Exception e) {
                e.printStackTrace();
            }
            if (billCount <= 0) {
                coupons_hint.setVisibility(View.VISIBLE);
            }else {
                coupons_hint.setVisibility(View.GONE);
            }
			break;
		default:
			break;
		}
	}

    @Override
    protected void callBackForServerFailed(HemaNetTask netTask,
                                           HemaBaseResult baseResult) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败"+baseResult.getMsg());
                break;
        }
    }

    @Override
    protected void callBackForGetDataFailed(HemaNetTask netTask, int failedType) {
        BaseHttpInformation information = (BaseHttpInformation) netTask
                .getHttpInformation();
        switch (information) {
            case ACCOUNT_GET:
                XtomToastUtil.showShortToast(mContext, "获取信息失败,请稍后再试");
                break;
        }
    }

	@Override
	protected void findView() {
		titleLeft = (ImageButton) findViewById(R.id.title_left_btn);
		title = (TextView) findViewById(R.id.title_text);
		titleRight = (ImageView) findViewById(R.id.title_right_img);
		right = (Button) findViewById(R.id.title_right_btn);

		total_fee = (TextView) findViewById(R.id.total_fee);
		coupons_hint = (TextView) findViewById(R.id.coupons_hint);
		tv_get_records = (TextView) findViewById(R.id.tv_get_records);
		tv_pay_records = (TextView) findViewById(R.id.tv_pay_records);
		ll_family = (LinearLayout) findViewById(R.id.ll_family);
		ll_zhuanzeng = (LinearLayout) findViewById(R.id.layout_tranfer);
		ll_chongzhi = (LinearLayout) findViewById(R.id.layout_charge);
		tv_family_count = (TextView) findViewById(R.id.tv_family_count);
	}

	@Override
	protected void getExras() {
	}

	@Override
	protected void setListener() {
		title.setText("我的粮票");
		titleLeft.setOnClickListener(this);
		right.setVisibility(View.GONE);
		titleRight.setVisibility(View.VISIBLE);
		titleRight.setImageResource(R.drawable.info);
		titleRight.setOnClickListener(this);
		tv_get_records.setOnClickListener(this);
		tv_pay_records.setOnClickListener(this);
		ll_family.setOnClickListener(this);
		ll_zhuanzeng.setOnClickListener(this);
		ll_chongzhi.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_left_btn:
                finish();
                break;
            case R.id.title_right_img:
                Intent itAbout = new Intent(mContext, WebviewActivity.class);
                itAbout.putExtra("parm", "coupon");
                itAbout.putExtra("type", 4);
                startActivity(itAbout);
                break;
            case R.id.tv_get_records:
                Intent itGet = new Intent(mContext, FoodStampGainListActivity.class);
                startActivity(itGet);
                break;
            case R.id.tv_pay_records://消费记录
                Intent itPay = new Intent(mContext, FoodStampCostListActivity.class);
				itPay.putExtra("type", 1);
                startActivity(itPay);
                break;
            case R.id.ll_family:
                Intent itFamily = new Intent(mContext, RankingActivity.class);
                startActivity(itFamily);
                break;
            case R.id.layout_tranfer:
                Intent itZhuanzeng = new Intent(mContext, TransferActivity.class);
                startActivity(itZhuanzeng);
                break;
            case R.id.layout_charge:
                Intent itCharge = new Intent(mContext, ChargeMoneyActivity.class);
                itCharge.putExtra("type", 2);
                startActivity(itCharge);
                break;
            default:
                break;
        }
    }

	@Subscribe
	public void onEventMainThread(EventBusMsg msg) {
		if (msg.getType() == EventBusConfig.UPDATE_INFO) {
            init();
		}
	}
}